using Microsoft.Graphics.Canvas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Windows;
using Windows.UI;

namespace DK.Baekdorf.MathTexRender
{


    // Box representing single character.
    internal class CharBox : Box
    {
        public CharBox(TexEnvironment environment, CharInfo charInfo)
            : base(environment)
        {
            this.Character = charInfo;
            this.Width = charInfo.Metrics.Width;
            this.Height = charInfo.Metrics.Height;
            this.Depth = charInfo.Metrics.Depth;
        }

        public CharInfo Character
        {
            get;
            private set;
        }

        public override void Draw(CanvasDrawingSession drawingContext, double scale, double x, double y)
        {
            // Draw character at given position.
            var typeface = this.Character.Font;
#warning Need test
            // CB: Changed
            typeface.FontSize = (float)(this.Character.Size * scale);
            
            // CB: This is bad, using hardcoded contant is get baseline!!! Can we find a better solution???
            var baseLine = 0.772 * this.Character.Size * scale;
            var ch = this.Character.Character;
            drawingContext.DrawText(this.Character.Character.ToString(), (float)(x * scale), (float)(y * scale - baseLine), this.Foreground ?? Colors.Black, typeface);


            // CB: Old code
            //var glyphIndex = typeface.CharacterToGlyphMap[this.Character.Character];
            //var glyphRun = new GlyphRun(typeface, 0, false, this.Character.Size * scale,
            //    new ushort[] { glyphIndex }, new Point(x * scale, y * scale),
            //    new double[] { typeface.AdvanceWidths[glyphIndex] }, null, null, null, null, null, null);
            //drawingContext.DrawGlyphRun(this.Foreground ?? Brushes.Black, glyphRun);
        }

        public override int GetLastFontId()
        {
            return this.Character.FontId;
        }
    }

}
